package com.ssy.lingxi.report.serviceimpl;

import cn.hutool.core.date.DateUtil;
import com.ssy.lingxi.common.constant.member.MemberRoleEnum;
import com.ssy.lingxi.common.model.dto.UserLoginCacheDTO;
import com.ssy.lingxi.common.response.ResponseCode;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.report.api.enums.*;
import com.ssy.lingxi.report.entity.report.MemberTallyReportDO;
import com.ssy.lingxi.report.entity.report.NewlyAddedDayReportDO;
import com.ssy.lingxi.report.model.bo.MemberTallyReportBO;
import com.ssy.lingxi.report.model.vo.response.*;
import com.ssy.lingxi.report.repository.report.NewlyAddedDayReportRepository;
import com.ssy.lingxi.report.service.IPlatformHomeService;
import com.ssy.lingxi.report.service.tally.ITallyReportService;
import com.ssy.lingxi.report.utils.TimeUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * 平台首页接口实现
 * @author 伍将
 * @date 2020/11/24
 * @version 2.0.0
 */
@Service
public class PlatformHomeServiceImpl implements IPlatformHomeService {

    @Resource
    private ITallyReportService tallyReportService;

//    @Resource
//    private PlatformTallyReportRepository repository;

    @Resource
    private NewlyAddedDayReportRepository newlyAddedDayReportRepository;

    /**
     * 获取交易与售后统计计数
     * @param user 登录用户
     * @return 交易与售后统计计数
     */
    @Override
    public Wrapper<TradeAndAfterSaleTallyVO> getTradeAndAfterSaleTally(UserLoginCacheDTO user) {
        TradeAndAfterSaleTallyVO result = new TradeAndAfterSaleTallyVO();
        // 平台账户查询时，默认会员与角色id为0
        MemberTallyReportDO reportDO = tallyReportService.getTallyDetail(0L, 0L);
        if (reportDO == null) {
            reportDO = new MemberTallyReportDO();
        }
        if (reportDO.getTallyReport() == null) {
            MemberTallyReportBO memberTallyReportBO = new MemberTallyReportBO();
            reportDO.setTallyReport(memberTallyReportBO);
        }

        // 交易
        List<TallyReportItemVO> itemVOList = new ArrayList<>();
        // 待审核需求单
        TallyReportItemVO itemVO = new TallyReportItemVO();
        itemVO.setName(DemandPublishOperateTypeEnum.TO_BE_VALIFY.getName());
        itemVO.setLink("/demandOrder/demandOrderAudit");
        itemVO.setCount(reportDO.getTallyReport().getTradeDemandPublish() != null && reportDO.getTallyReport().getTradeDemandPublish().getToBeValifyCount() > 0 ? reportDO.getTallyReport().getTradeDemandPublish().getToBeValifyCount() : 0L);
        itemVOList.add(itemVO);
        // 待确认支付结果订单
        itemVO = new TallyReportItemVO();
        itemVO.setName(SaleOrderOperateTypeEnum.TO_BE_CONFIRM_PAY_RESULT.getName());
        itemVO.setLink("/orderSystem/readyConfirmPayList");
        itemVO.setCount(reportDO.getTallyReport().getTradeSaleOrder() != null && reportDO.getTallyReport().getTradeSaleOrder().getToBeConfirmPayResultCount() > 0 ? reportDO.getTallyReport().getTradeSaleOrder().getToBeConfirmPayResultCount() : 0L);
        itemVOList.add(itemVO);
        result.setTradeList(itemVOList);

        // 售后
        itemVO = new TallyReportItemVO();
        itemVO.setName(AfterSaleReturnHandleOperateTypeEnum.TO_BE_REFUND.getName());
        itemVO.setLink("/afterService/returnManage/returnPrReturn");
        itemVO.setCount(reportDO.getTallyReport().getAfterSaleReturnHandle() != null && reportDO.getTallyReport().getAfterSaleReturnHandle().getToBeRefundCount() > 0 ? reportDO.getTallyReport().getAfterSaleReturnHandle().getToBeRefundCount() : 0L);
        itemVOList = new ArrayList<>();
        itemVOList.add(itemVO);
        result.setAfterSaleList(itemVOList);

        return Wrapper.success(result);
    }

    /**
     * 获取会员代办计数
     * @param user 登录用户
     * @return 会员代办计数
     */
    @Override
    public Wrapper<List<TallyReportItemVO>> getMemberTally(UserLoginCacheDTO user) {

        List<TallyReportItemVO> resultList = null;
        Wrapper<TallyReportVO> tallyReportVOWrapper = tallyReportService.getTallyReport(user);
        if (tallyReportVOWrapper == null || tallyReportVOWrapper.getCode() != ResponseCode.SUCCESS.getCode()) {
            return Wrapper.fail(Optional.ofNullable(tallyReportVOWrapper).orElse(Wrapper.fail(ResponseCode.BUSINESS_ERROR)));
        }
        if (tallyReportVOWrapper.getData() != null && tallyReportVOWrapper.getData().getMember() != null) {
            resultList = tallyReportVOWrapper.getData().getMember().getPlatformList();
        }

        return Wrapper.success(resultList);
    }

    /**
     * 获取商品计数
     * @param user 登录用户
     * @return 商品计数
     */
    @Override
    public Wrapper<PlatformCommodityTallyVO> getCommodityTally(UserLoginCacheDTO user) {

        PlatformCommodityTallyVO result = new PlatformCommodityTallyVO();

        // 平台账户查询时，默认会员与角色id为0
        MemberTallyReportDO report = tallyReportService.getTallyDetail(0L, 0L);
        if (report == null) {
            report = new MemberTallyReportDO();
        }
        if (report.getTallyReport() == null) {
            MemberTallyReportBO memberTallyReportBO = new MemberTallyReportBO();
            report.setTallyReport(memberTallyReportBO);
        }

        // 全部商品
        result.setProductCount(report.getTallyReport().getCommodity() != null && report.getTallyReport().getCommodity().getProductCount() > 0 ? report.getTallyReport().getCommodity().getProductCount() : 0L);
        // 全部品牌
        result.setBrandCount(report.getTallyReport().getCommodity() != null && report.getTallyReport().getCommodity().getBrandCount() > 0 ? report.getTallyReport().getCommodity().getBrandCount() : 0L);
        // 待审核商品
        TallyReportItemVO itemVO = new TallyReportItemVO();
        itemVO.setName(CommodityOperateTypeEnum.TO_BE_PRODUCT_CHECK_COMMIT.getName());
        itemVO.setLink(CommodityOperateTypeEnum.TO_BE_PRODUCT_CHECK_COMMIT.getLink());
        itemVO.setCount(report.getTallyReport().getCommodity() != null && report.getTallyReport().getCommodity().getToBeProductValifyCount() > 0 ? report.getTallyReport().getCommodity().getToBeProductValifyCount() : 0L);
        result.setToBeProductValify(itemVO);
        //  待审核品牌
        itemVO = new TallyReportItemVO();
        itemVO.setName(CommodityOperateTypeEnum.TO_BE_BRAND_CHECK_COMMIT.getName());
        itemVO.setLink(CommodityOperateTypeEnum.TO_BE_BRAND_CHECK_COMMIT.getLink());
        itemVO.setCount(report.getTallyReport().getCommodity() != null && report.getTallyReport().getCommodity().getToBeBrandValifyCount() > 0 ? report.getTallyReport().getCommodity().getToBeBrandValifyCount() : 0L);
        result.setToBeBrandValify(itemVO);

        // 查询当天数据
        LocalDateTime startTime = TimeUtil.parseStringToDateTime(DateUtil.date().toString("yyyy-MM-dd 00:00:00"), "yyyy-MM-dd HH:mm:ss");
        NewlyAddedDayReportDO newlyAddedDayReportDO = newlyAddedDayReportRepository.getAllByDateTimeBetween(startTime, startTime).stream().findFirst().orElse(null);
        itemVO = new TallyReportItemVO();
        itemVO.setName("今日新增商品");
        itemVO.setLink("/productManage/commodity/products");
        itemVO.setCount(newlyAddedDayReportDO != null && newlyAddedDayReportDO.getCommodityCount() > 0 ? newlyAddedDayReportDO.getCommodityCount() : 0L);
        result.setAddProduct(itemVO);
        itemVO = new TallyReportItemVO();
        itemVO.setName("今日新增品牌");
        itemVO.setLink("/productManage/trademark/trademarkSearch");
        itemVO.setCount(newlyAddedDayReportDO != null && newlyAddedDayReportDO.getBrandCount() > 0 ? newlyAddedDayReportDO.getBrandCount() : 0L);
        result.setAddBrand(itemVO);


        return Wrapper.success(result);
    }

    /**
     * 获取资金与结算中心计数
     * @param user 登录用户
     * @return 资金与结算中心计数
     */
    @Override
    public Wrapper<AccountAndSettleAccountTallyVO> getAccountAndSettleAccountTally(UserLoginCacheDTO user) {
        AccountAndSettleAccountTallyVO result = new AccountAndSettleAccountTallyVO();

        MemberTallyReportDO reportDO = tallyReportService.getDetail(user);
        if (reportDO == null) {
            reportDO = new MemberTallyReportDO();
        }
        if (reportDO.getTallyReport() == null) {
            MemberTallyReportBO memberTallyReportBO = new MemberTallyReportBO();
            reportDO.setTallyReport(memberTallyReportBO);
        }
        TallyReportItemVO itemVO;

        // 平台结算数据，会员id与角色id为0
        MemberTallyReportDO platformReportDO = tallyReportService.getTallyDetail(0L, 0L);
        if (platformReportDO != null) {
            itemVO = new TallyReportItemVO();
            itemVO.setName("待付款代收账款结算");
            itemVO.setLink("/settlementManage/platformSettlement/balanced/accountPayable");
            itemVO.setCount(platformReportDO.getTallyReport().getSettleAccountPlatform() != null && platformReportDO.getTallyReport().getSettleAccountPlatform().getToBePayCount() > 0 ? platformReportDO.getTallyReport().getSettleAccountPlatform().getToBePayCount() : 0L);
            result.setToBePay(itemVO);
            itemVO = new TallyReportItemVO();
            itemVO.setName("已完成代收账款结算");
            itemVO.setLink("/settlementManage/platformSettlement/balanced/accountPayable");
            itemVO.setCount(platformReportDO.getTallyReport().getSettleAccountPlatform() != null && platformReportDO.getTallyReport().getSettleAccountPlatform().getCompleteCount() > 0 ? platformReportDO.getTallyReport().getSettleAccountPlatform().getCompleteCount() : 0L);
            result.setComplete(itemVO);
            itemVO = new TallyReportItemVO();
            itemVO.setName("待付款积分结算");
            itemVO.setLink("/settlementManage/platformSettlement/balanced/scorePayable");
            itemVO.setCount(platformReportDO.getTallyReport().getSettleAccountPlatformScore() != null && platformReportDO.getTallyReport().getSettleAccountPlatformScore().getToBePayCount() > 0 ? platformReportDO.getTallyReport().getSettleAccountPlatformScore().getToBePayCount() : 0L);
            result.setScoreToBePay(itemVO);
            itemVO = new TallyReportItemVO();
            itemVO.setName("已完成积分结算");
            itemVO.setLink("/settlementManage/platformSettlement/balanced/scorePayable");
            itemVO.setCount(platformReportDO.getTallyReport().getSettleAccountPlatformScore() != null && platformReportDO.getTallyReport().getSettleAccountPlatformScore().getCompleteCount() > 0 ? platformReportDO.getTallyReport().getSettleAccountPlatformScore().getCompleteCount() : 0L);
            result.setScoreComplete(itemVO);
        }

        itemVO = new TallyReportItemVO();
        itemVO.setName("待审核提现申请");
        itemVO.setLink("/settlementManage/capitalAccount/checkWithdraw");
        itemVO.setCount(reportDO.getTallyReport().getAsetAccount() != null && reportDO.getTallyReport().getAsetAccount().getTobeValifyCashoutCount() > 0 ? reportDO.getTallyReport().getAsetAccount().getTobeValifyCashoutCount() : 0L);
        result.setTobeValifyCashout(itemVO);
        itemVO = new TallyReportItemVO();
        itemVO.setName("待支付提现申请");
        itemVO.setLink("/settlementManage/capitalAccount/paymentWithdraw");
        itemVO.setCount(reportDO.getTallyReport().getAsetAccount() != null && reportDO.getTallyReport().getAsetAccount().getTobePayCashoutCount() > 0 ? reportDO.getTallyReport().getAsetAccount().getTobePayCashoutCount() : 0L);
        result.setTobePayCashout(itemVO);


        return Wrapper.success(result);
    }

    @Override
    public Wrapper<PurchaseTallyVO> getPurchaseTally(UserLoginCacheDTO sysUser) {
        PurchaseTallyVO result = new PurchaseTallyVO();
        // 平台账户查询时，默认会员与角色id为0
        MemberTallyReportDO reportDO = tallyReportService.getTallyDetail(MemberRoleEnum.PLATFORM.getMemberId(), MemberRoleEnum.PLATFORM.getRoleId());
        if (reportDO == null) {
            reportDO = new MemberTallyReportDO();
        }
        if (reportDO.getTallyReport() == null) {
            MemberTallyReportBO memberTallyReportBO = new MemberTallyReportBO();
            reportDO.setTallyReport(memberTallyReportBO);
        }

        // 交易
        List<TallyReportItemVO> itemVOList = new ArrayList<>();
        // 待审核采购询价 22.1.20版本去掉
        TallyReportItemVO itemVO = new TallyReportItemVO();
  /*      itemVO.setName(PlatformPurchaseOperateTypeEnum.TO_BE_VERIFY_INQUIRY_COUNT.getName());
        itemVO.setLink(PlatformPurchaseOperateTypeEnum.TO_BE_VERIFY_INQUIRY_COUNT.getLink());
        itemVO.setCount(reportDO.getTallyReport().getPlatformPurchase() != null && reportDO.getTallyReport().getPlatformPurchase().getToBeVerifyInquiryCount() > 0 ? reportDO.getTallyReport().getPlatformPurchase().getToBeVerifyInquiryCount() : 0L);
        itemVOList.add(itemVO);*/
        // 待审核采购竞价
//        itemVO = new TallyReportItemVO();
        itemVO.setName(PlatformPurchaseOperateTypeEnum.TO_BE_VERIFY_BIDDING_COUNT.getName());
        itemVO.setLink(PlatformPurchaseOperateTypeEnum.TO_BE_VERIFY_BIDDING_COUNT.getLink());
        itemVO.setCount(reportDO.getTallyReport().getTradeSaleOrder() != null && reportDO.getTallyReport().getPlatformPurchase().getToBeVerifyBiddingCount() > 0 ? reportDO.getTallyReport().getPlatformPurchase().getToBeVerifyBiddingCount() : 0L);
        itemVOList.add(itemVO);
        // 待审核招标项目 22.1.20版本去掉
   /*     itemVO = new TallyReportItemVO();
        itemVO.setName(PlatformPurchaseOperateTypeEnum.TO_BE_VERIFY_INVITE_TENDER_COUNT.getName());
        itemVO.setLink(PlatformPurchaseOperateTypeEnum.TO_BE_VERIFY_INVITE_TENDER_COUNT.getLink());
        itemVO.setCount(reportDO.getTallyReport().getAfterSaleReturnHandle() != null && reportDO.getTallyReport().getPlatformPurchase().getToBeVerifyInviteTenderCount() > 0 ? reportDO.getTallyReport().getPlatformPurchase().getToBeVerifyInviteTenderCount() : 0L);
        itemVOList.add(itemVO);*/
        result.setTodoList(itemVOList);

        return Wrapper.success(result);
    }
}
